<%@page import="ContentManagers.RoleManagers.PMManager"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<div id="product_manager_body" class="content_box">
    <div id="add_product">

            <form name ="addProduct" method="post" onsubmit="return validateAddProduct()" action="../ServletAddProduct">
            <br>Add Product!<br><smallerfont>
            <br>Product name:
            <br><input class="inputbox" type="text" value="" name="name" autocomplete ="off" onfocus="clearInitial(this, 'Username')"/>
            <br>Description:
            <br><input class="inputbox" type="text" value="" name="description" autocomplete ="off" onfocus="clearInitial(this, 'Password')"/>
            <br>Price:
            <br><input class="inputbox" type="text" value="" name="price" autocomplete ="off" onfocus="clearInitial(this, 'Password')"/>
            <br><input type="submit" name="Submit" value="Submit"/>
            <br></smallerfont>
            <%
             if(session.getAttribute("message")!=null) {
             
             if(session.getAttribute("message").equals("1"))
                out.println("\nInvalid product name.\n");
             else if(session.getAttribute("message").equals("2"))
                out.println("\nInvalid product description.\n");
             else if(session.getAttribute("message").equals("3"))
                out.println("\nInvalid price value.\n");
             else if (session.getAttribute("message").equals("4"))
                out.println("\nError in adding product. <br>Please check database connection\n");
             else if (session.getAttribute("message").equals("5"))
                out.println("\nProduct already exists in the inventory.\n");
             else if (session.getAttribute("message").equals("6"))
                out.println("\nProduct is successfully <br>added to the inventory.\n");
             
             session.removeAttribute("message");
             }
            %>
            </form>
    </div>


                    
                    

            <div id ="product_list">                    
                <%
                out.println("List of Products<br><br>");


                ResultSet rs = new PMManager().getProductList(Integer.parseInt(session.getAttribute("managerType").toString()));
                //check if no valid row / move forward
                if(rs == null || rs.first() == false){
                    out.println("No products of this type.<br><br>");
                }
                else {
                    //restart from before first row
                    rs.beforeFirst();

                    int indexNo = 0;
                    ArrayList indexer = new ArrayList();

                    while(rs.next())
                    {
                    indexer.add(indexNo, rs.getString("product_id"));
                    %>
                    <div class="manageProduct">
                        <form name ="expiredPassword" method="post" onsubmit="" action="../ServletManageProduct">
                            <input type="text" name="productIndex" value="<%out.println(indexNo);%>" hidden/>
                            <table class="product_table"><tr><td><b>Product Name:</b></td>
                            <td><input type="text" name="product_name" value="<%out.println(rs.getString("product_name") + "\""); if(rs.getString("isAvailable").equals("0")) out.println("disabled"); %>/></td></tr>
                            <tr><td><b>Product Description:</b> </td>
                            <td><input type="text" name="description" value="<%out.println(rs.getString("description") + "\""); if(rs.getString("isAvailable").equals("0")) out.println("disabled"); %>/></td></tr>
                            <tr><td><b>Price:</b> </td>
                            <td><input type="text" name="price" value="<%out.println(rs.getString("price") + "\""); if(rs.getString("isAvailable").equals("0")) out.println("disabled"); %>/></td></tr>
                            <tr><td></td><td><input type="submit" id="submit" name="SubmitManage" value="Update" <%if(rs.getString("isAvailable").equals("0")) out.println("disabled"); %> />
                                           
                            <%if(rs.getString("isAvailable").equals("1"))
                            {%>
                               <input type="submit" id="submit" name="SubmitManage" value="Delete"/></td></tr></table>
                            <% }
                             else
                            { %>
                               <input type="submit" id="submit" name="SubmitManage" value="Restore Item"/></td></tr></table> 
                            <% }
                                       
                        if((session.getAttribute("errorMessage"))!=null)
                        {
                            int formNo = Integer.parseInt(session.getAttribute("formNo").toString());
                            if((session.getAttribute("errorMessage"))== "1" && formNo==indexNo)
                                out.println("Invalid Product Name.");
                            else if((session.getAttribute("errorMessage"))== "2" && formNo==indexNo)
                                out.println("Invalid Product Description.");
                            else if((session.getAttribute("errorMessage"))== "3" && formNo==indexNo)
                                out.println("Invalid Price.");
                            else if((session.getAttribute("errorMessage"))== "4" && formNo==indexNo)
                                out.println("Edit successful.");
                            else if((session.getAttribute("errorMessage"))== "5" && formNo==indexNo)
                                out.println("A database error has occured.");
                            else if((session.getAttribute("errorMessage"))== "6" && formNo==indexNo)
                                out.println("Another product already <br>exists with the same name.");

                            session.removeAttribute("errorMessage");
                        }
                            %>
                        </form>
                        </div>
                        <br>
                    <%

                    indexNo++;
                    }
                    //pass the array using session
                    request.getSession().setAttribute("indexer", indexer);

                }
                %>
                </div>
</div>